<template>
	<view class="batch-manage">
		<scroll-view class="batch-manage-list" scroll-y @scrolltolower="loadMore">
			<checkbox-group v-model="choseList" @change="change" v-if="List.length">
				<view class="batch-manage-item" v-for="(data,index) in List" :key="index">
					<label>
						<checkbox :value="(data.store_goods_id).toString()" :checked="data.checked" color="#FFF" />
						<image class="batch-img" :src="data.image_middle" mode="aspectFill"></image>
						<view class="batch-manage-info">
							<view class="batch-manage-info-tit">{{data.store_goods_name}}</view>
							<view class="batch-manage-info-text">市场价：¥{{data.market_price}}</view>
							<view class="batch-manage-info-text">零售价：¥{{data.retail_price}}</view>
							<view class="batch-manage-info-text">成本价：¥{{data.factory_price}}</view>
							<view class="batch-manage-info-text">
								<text>总销量：<text>{{data.sell_number}}</text></text>
								<text>库存：<text>{{data.num}}</text></text>
							</view>
						</view>
					</label>
				</view>
			</checkbox-group>
			<nothing :text="is_package?'没有套餐':'没有商品'" :btn="false" @goUrl="add" v-else></nothing>
		</scroll-view>
		<view class="batch-manage-bottom" v-if="List.length">
			<checkbox-group @change="allChose">
				<label><checkbox color="#FFF" :checked="isAll" value="all"/>{{isAll?'取消全选':'全选'}}</label>
			</checkbox-group>
			<view class="batch-manage-bottom-tab">
				<view @click="down" v-if="type + 1==1">下架商品</view>
				<view @click="up" v-else>上架商品</view>
				<view @click="del">删除商品</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				List: [],
				choseList: [],
				allChecked: [],
				isAll: false,
				type: '',
				categoryID: '',
				is_package: false,
				page: 0,
				noMore: false
			};
		},
		onLoad(options) {
			if(options.type){
				uni.setNavigationBarTitle({
					title: options.type==0?'管理出售中商品':'管理已下架商品'
				})
			}
			if(options.is_package){
				this.is_package = true
			}
			if(options.type!=null){
				this.type = options.type
			}
			if(options.categoryID){
				this.categoryID = options.categoryID
			}
			this.init(0,options.type,options.categoryID)
		},
		methods: {
			loadMore(){
				if(!this.noMore){
					this.page++
					this.init(this.page,this.type)
				}
			},
			init(page,type,class_id){
				let data = {
					limit: 10,
					page: page?page:0,
					order_by: 'add_time',
					asc: 1,
					store_goods_id: '',
					store_goods_sn: '',
					user_id:  JSON.parse(uni.getStorageSync('userInfo')).user_id,
					class_id: class_id?class_id:'',
					is_sale: Number(type) + 1,
					goods_type: '',
					store_id: JSON.parse(uni.getStorageSync('userInfo')).store_id,
					paramter: '',
					type: this.is_package?2:1
				}
				this.$api.StoreGoodsList(data).then(res => {
					if(res.datas.length){
						res.datas.forEach(item => {
							this.List.push(item)
						})
					}else{
						this.noMore = true
					}
				})
			},
			change(e){
				let _this = this
				var items = this.List,
						values = e.detail.value;
				for (var i = 0, lenI = items.length; i < lenI; ++i) {
					const item = items[i]
					if(values.includes(item.store_goods_id.toString())){
						this.$set(item,'checked',true)
					}else{
						this.$set(item,'checked',false)
					}
				}
				let arr = []
				this.List.forEach((item,index) => {
					if(item.checked){
						arr.push(item.store_goods_id)
					}
				})
				if(arr.length==this.List.length){
					this.isAll = true
				}else{
					this.isAll = false
				}
			},
			allChose(e){
				if(e.detail.value.length){
					this.isAll = true
					this.List.forEach((item,index) => {
						this.$set(item,'checked',true)
					})
				}else{
					this.isAll = false
					this.List.forEach((item,index) => {
						this.$set(item,'checked',false)
					})
				}
			},
			down(){
				let select = [],
						_this = this;
				this.List.forEach(item => {
					if(item.checked){
						select.push(item.store_goods_id)
					}
				})
				if(!select.length){
					uni.showToast({
						icon: "none",
						title: "请选择需要下架的商品"
					})
					return
				}
				uni.showModal({
					content: "是否确认下架选择的商品？",
					success(res) {
						if(res.confirm){
							let data = {
								store_goods_id: select.join(','),
								is_sale: 2
							}
							_this.$api.SoldOutGoods(data).then(res => {
								uni.showToast({
									icon: "none",
									title: "下架成功",
									duration: 1500
								})
								setTimeout(() => {
									_this.List = []
									_this.init(0,_this.type,_this.categoryID)
								},1500)
							})
						}
					}
				})
			},
			up(){
				let select = [],
						_this = this;
				this.List.forEach(item => {
					if(item.checked){
						select.push(item.store_goods_id)
					}
				})
				if(!select.length){
					uni.showToast({
						icon: "none",
						title: "请选择需要上架的商品"
					})
					return
				}
				uni.showModal({
					content: "是否确认上架选择的商品?",
					success(res) {
						if(res.confirm){
							let data = {
								store_goods_id: select.join(','),
								is_sale: 1
							}
							_this.$api.SoldOutGoods(data).then(res => {
								uni.showToast({
									icon: "none",
									title: "上架成功",
									duration: 1500
								})
								setTimeout(() => {
									_this.List = []
									_this.init(0,_this.type,_this.categoryID)
								},1500)
							})
						}
					}
				})
			},
			del(){
				let select = [],
						_this = this;
				this.List.forEach(item => {
					if(item.checked){
						select.push(item.store_goods_id)
					}
				})
				if(!select.length){
					uni.showToast({
						icon: "none",
						title: "请选择需要删除的商品"
					})
					return
				}
				uni.showModal({
					content: "是否确认删除选择的商品?",
					success(res) {
						if(res.confirm){
							let data = {
								store_goods_id: select.join(',')
							}
							_this.$api.DelateGoods(data).then(res => {
								uni.showToast({
									icon: "none",
									title: "删除成功",
									duration: 1500
								})
								setTimeout(() => {
									_this.List = []
									_this.init(0,_this.type,_this.categoryID)
								},1500)
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	@import '../../static/scss/pro-batch-manage.scss'
</style>